<?php

require_once 'DBConfig.php';
require_once 'Database.php';


class UserDAL{
	
	private $db;
	private $userdal;
	
	public function __construct(Database $db){
		$this->db = $db;
		
 	}
	
	/**
	 * Funktion för att hämta alla användare i databasen
	 * @param $sqlQuery The sql to get all users
	 * @return array Array containing Users
	 */
	
	public function SelectUsers($sqlQuery){
		
		$stmt = $this->$db->Prepare($sqlQuery);
		$this->$db->Execute($stmt);

		$stmt->bind_result($id, $user, $pass);

		$ret = array();
		$user = array();
		while ($stmt->fetch()) {
			$user['ID'] = $id;	
			$user['Username'] = $user;
			$user['Password'] = $pass;
			$ret[] = $user;
		}
		$stmt->close();
		var_dump($ret);
		return $ret;
	}
	
	public function CountUsers($sqlQuery){
	
		$stmt = $this->db->Prepare($sqlQuery);
		$this->db->Execute($stmt);
				
		$ret = 0;
		$stmt->bind_result($ret);
		$stmt->fetch();
		$stmt->close();
		
		return $ret;
	}
	
	/**
	 * Funktion för att hämta en användare
	 */	
	public function SelectOneUserByUsernameAndPassword($username, $password){

		$sqlQuery = "SELECT * FROM inserttable WHERE Username = ? AND Password = ?";

		$stmt = $this->db->Prepare($sqlQuery);
		
		$stmt->bind_param('ss', $username, $password);
	
		$this->db->Execute($stmt);
				
		$stmt->bind_result($id, $userName, $pass);

		$user = array();
		while ($stmt->fetch()) {
			$user['ID'] = $id;
			$user['Username'] = $userName;
			$user['Password'] = $pass;
		}
		$stmt->close();
		return $user;
	}
	
	//Skapar tester för att koll att databasen fungerar korrekt
	public static function test(DBConfig $dbConfig){
		$db = new Database();	
		$userdal = new UserDAL($db);
		
		if($db->Connect($dbConfig) == FALSE){
			echo "Connection to database failed <br>";
			return FALSE;
		}
		
		//Testar så att det går att lägga till användare i databasen.
		$nrPostsBefore = $userdal->CountUsers("SELECT COUNT(*) FROM inserttable");
			
		$stmt = $db->Prepare("INSERT INTO inserttable (Username, Password) VALUES (?,?)");
		$user = 'test';
		$pass = 'pass';
		$stmt->bind_param('ss', $user, $pass);
		$insertId = $db->Insert($stmt);
			
		$nrPostsAfter = $userdal->CountUsers("SELECT COUNT(*) FROM inserttable");
		if ($nrPostsAfter != $nrPostsBefore + 1) {
			echo "Insert failed.";
			return false;
		}

		//Testar så vi kan hämta en användare på sitt id
		$stmt = $db->Prepare("SELECT user_id, Username, Password FROM inserttable WHERE user_id = ?");
		$stmt->bind_param('i', $insertId);
		$fetchedUser = $userdal->SelectOneUserByUsernameAndPassword($stmt);

		if ($fetchedUser['ID'] !== $insertId) {
			echo "SelectOneUser failed";
			return false;
		}
		
		//Testar att ta bort en användare
		$stmt = $db->Prepare("DELETE FROM inserttable WHERE user_id = ?");
		$stmt->bind_param('i', $insertId);
		$db->Execute($stmt);

		//Testar så att det går att välja en användare.
		$nrPostsAfter = $userdal->CountUsers("SELECT COUNT(*) FROM inserttable");
		if ($nrPostsBefore !== $nrPostsAfter) {
			echo "Database Delete user failed.";
			return false;
		}
		
		if($db->Close() == FALSE){
			echo "Close database failed";
			return FALSE;
		}
		
		echo "Test av databas lyckades <br>";
		return TRUE;
	}
}
